package com.wing.order.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.enterprise.product.model.response.ProductPackLabelVO;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.order.model.entity.DistributionDemand;
import com.wing.order.model.response.DistributionDemandVO;
import com.wing.order.service.DistributionDemandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.order.model.request.DistributionDemandForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-配货需求订单表管理", tags = "配货需求订单表管理")
@RestController("appDistributionDemandController")
@RequestMapping("order/app/distributionDemand")
@Slf4j
public class DistributionDemandController {

    @Autowired
    private DistributionDemandService distributionDemandService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "领货列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "单号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<DistributionDemandVO> list(
            @RequestParam(name = "page",  defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit",  defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort",  defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order", defaultValue = "desc", required = false) String order
            ,String code
            ,String queryFlag
    ) {
        Page<DistributionDemandVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("code",code);
        params.put("queryFlag",queryFlag);
        Page<DistributionDemandVO> list = distributionDemandService.pageListReceive4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "配货需求单列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "单号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/listDemand")
    public PageResult<DistributionDemandVO> listDemand(
            @RequestParam(name = "page",  defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit",  defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort",  defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order", defaultValue = "desc", required = false) String order
            ,String code
            ,String queryFlag
    ) {
        Page<DistributionDemandVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("code",code);
        params.put("queryFlag",queryFlag);
        Page<DistributionDemandVO> list = distributionDemandService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long distributionDemandId) {
        DistributionDemandVO distributionDemandVO = distributionDemandService.get4appById(distributionDemandId);
        if (distributionDemandVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), distributionDemandVO);
    }

    @PostMapping()
    public JsonResult add(@Validated DistributionDemandForm distributionDemandForm) {
        if (distributionDemandService.add(distributionDemandForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

    @ApiOperation(value = "获取包装条码的数据", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "detailId", value = "detailId", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "labelCode", value = "单号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/getPackLabel")
    public JsonResult<ProductPackLabelVO> getPackLabelVo(Long id,Long detailId,String labelCode){
        try{
            return distributionDemandService.getPackLabel(id,detailId,labelCode);
        }catch (Exception e){
            log.error("get label fail",e);
            return JsonResult.fail("获取包装条码失败");
        }
    }

}
